﻿using Gallery.Datas.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Gallery.Datas
{
	public class MoneyFundIncomeDbService
	{
		public static readonly MoneyFundIncomeDbService Instance = new MoneyFundIncomeDbService();

		public List<MoneyFundIncome> GetLatestIncomes()
		{
			using (GalleryEntities db = new GalleryEntities())
			{
				List<MoneyFundIncome> result = new List<MoneyFundIncome>();
				var groupQuery = db.MoneyFundIncomes.GroupBy(p => p.FundId);
				foreach (var item in groupQuery)
				{
					result.Add(item.OrderByDescending(p => p.Date).First());
				}
				return result.OrderByDescending(p => p.Date).ThenByDescending(p => p.Yield).ToList();
			}
		}

		public List<MoneyFundIncome> GetIncomes(int fundId, int pageSize, int pageIndex)
		{
			using (GalleryEntities db = new GalleryEntities())
			{
				return db.MoneyFundIncomes.Where(p => p.FundId == fundId).OrderByDescending(p=>p.Date).Skip((pageIndex - 1) * pageSize).ToList();
			}
		}
	}
}
